package controllers

import (
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	"demo0606/modules"
)

type Role struct{}

func (role Role) AddRole(c *gin.Context) {
	roleName := c.PostForm("role_name")
	description := c.PostForm("description")
	// auth := c.PostForm("auth")
	now := time.Now().Unix()
	time2 := int(now)
	roleDat := modules.Role{
		RoleName:    roleName,
		Description: description,
		// Auth:        auth,
		AddTime: time2,
	}
	err := modules.DB.Create(&roleDat).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "新增角色成功",
		})
	} else {
		c.JSON(500, gin.H{
			"success": false,
			"message": "新增角色失败",
		})
	}

}
func (role Role) DeleteRole(c *gin.Context) {
	id := c.Query("id")
	idN, _ := strconv.Atoi(id)
	roleData := modules.Role{}
	roleData.Id = idN
	println("id", id)
	err := modules.DB.Delete(&modules.Role{}, id).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "删除角色成功",
		})
	} else {
		c.JSON(500, gin.H{
			"success": false,
			"message": "删除角色失败",
		})
	}

}
func (role Role) UpdateRole(c *gin.Context) {
	id := c.PostForm("id")
	roleName := c.PostForm("role_name")
	auth := c.PostForm("auth")
	description := c.PostForm("description")
	err := modules.DB.Model(&modules.Role{}).Where("Id=?", id).Updates(map[string]interface{}{
		"RoleName":    roleName,
		"Auth":        auth,
		"Description": description,
	}).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "修改角色成功",
		})
	} else {
		c.JSON(500, gin.H{
			"success": false,
			"message": "修改角色失败",
		})
	}
}
func (role Role) GetRoleList(c *gin.Context) {
	roleName := c.DefaultQuery("role_name", "")
	page := c.DefaultQuery("page", "1")
	pageSize := c.DefaultQuery("pageSize", "10")
	var total int64
	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	roleList := []modules.Role{}
	if roleName == "" {
		err := modules.DB.Preload("Auths").Find(&roleList).Limit(ps).Offset((p - 1) * ps).Limit(-1).Offset(-1).Count(&total).Error
		if err == nil {
			c.JSON(200, gin.H{
				"success":  true,
				"message":  "获取角色列表",
				"page":     page,
				"pageSize": pageSize,
				"total":    total,
				"list":     roleList,
			})
		} else {
			c.JSON(500, gin.H{
				"success": true,
				"message": "获取角色列表失败",
			})
		}

	} else {
		err := modules.DB.Preload("Auths").Where("role_name LIKE ?", "%"+roleName+"%").Find(&roleList).Limit(-1).Offset(-1).Count(&total).Error
		if err == nil {
			c.JSON(200, gin.H{
				"success": true,
				"message": "获取角色列表",
				"total":   total,
				"list":    roleList,
			})
		} else {
			c.JSON(500, gin.H{
				"success": false,
				"message": "获取角色列表失败",
			})
		}

	}

}

func (role Role) AddAuth(c *gin.Context) {
	roleId := c.PostForm("role_id")
	authId := c.PostForm("auth_id")
	roleIdInt, _ := strconv.Atoi(roleId)
	auth_list := strings.Split(authId, ",")
	roleAuth := modules.RoleAuth{}
	err1 := modules.DB.Where("role_id=?", roleId).Delete(&roleAuth).Error
	for _, v := range auth_list {
		roleAuth := modules.RoleAuth{}
		roleAuth.RoleId = roleIdInt
		n, _ := strconv.Atoi(v)
		roleAuth.AuthId = n
		modules.DB.Create(&roleAuth)
	}
	if err1 != nil {
		c.JSON(200, gin.H{
			"success": false,
			"message": "修改权限失败",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "修改权限成功",
		})
	}

}
